package com.zy.sky.cloud;

import com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.core.env.StandardEnvironment;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * Package: java.com.zy.sky.cloud
 * <p>
 * Author: zy
 * <p>
 * Date: Created in 2019-3-11 11:16
 */
public class AdminTestApplication {

    public static void main(String[] args) {
        // 对应application-dev.yml 中配置的根密码
        System.setProperty("jasypt.encryptor.password", "sky");
        StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());

        //加密方法
        System.out.println(stringEncryptor.encrypt("sky"));

        System.out.println("base64加密>>>>" + encodeByBase64("sky:sky"));
        System.out.println("base64解密>>>>" + decodeByBase64(encodeByBase64("sky:sky")));

        //解密方法
        System.out.println(stringEncryptor.decrypt("ltJPpR50wT0oIY9kfOe1Iw==="));


    }

    /***
     * base64加密
     * @param str
     * @return
     */
    public static String encodeByBase64(String str){
        try {
            return new String(Base64.getEncoder().encode(str.getBytes("UTF-8")),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base64解密
     * @param str
     * @return
     */
    public static String decodeByBase64(String str){
        try {
            return new String(Base64.getDecoder().decode(str.getBytes("UTF-8")),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
